<template>
  <div class="layout-container">
    <el-container>
      <!-- 顶部导航栏 -->
      <el-header height="80px" class="top-header">
        <div class="header-left">
          <div class="logo">
            <h2>护康AI贴心助手</h2>
          </div>
          
          <el-menu
            :default-active="activeMenu"
            mode="horizontal"
            class="top-menu"
            router
            background-color="#304156"
            text-color="#bfcbd9"
            active-text-color="#409EFF"
          >
            <el-menu-item 
              v-for="route in menuRoutes" 
              :key="route.path"
              :index="route.path"
            >
              <el-icon><component :is="route.meta.icon" /></el-icon>
              <template #title>{{ route.meta.title }}</template>
            </el-menu-item>
          </el-menu>
        </div>
        
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-avatar :size="32">{{ (userStore.realName || userStore.username).slice(-1) }}</el-avatar>
              <span class="username">{{ userStore.realName || userStore.username }}</span>
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>个人信息
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <!-- 内容区域 -->
      <el-main class="main-content">
        <div class="breadcrumb-container">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-if="currentRoute.meta.title">
              {{ currentRoute.meta.title }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        
        <div class="content-wrapper">
          <router-view />
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import { UserFilled, ArrowDown, User, SwitchButton } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

// 当前激活的菜单
const activeMenu = computed(() => route.path)

// 当前路由信息
const currentRoute = computed(() => route)

// 菜单路由
const menuRoutes = computed(() => {
  return router.getRoutes()
    .filter(route => route.meta && route.meta.title && route.path !== '/' && !route.path.includes(':'))
    .sort((a, b) => {
      const order = ['dashboard', 'profile', 'health', 'diet', 'exercise', 'ai-advice', 'charts']
      return order.indexOf(a.name) - order.indexOf(b.name)
    })
})

// 处理下拉菜单命令
const handleCommand = (command) => {
  if (command === 'profile') {
    router.push('/profile')
  } else if (command === 'logout') {
    ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      userStore.logout()
      ElMessage.success('已退出登录')
      router.push('/login')
    }).catch(() => {})
  }
}

// 确保用户信息已加载
onMounted(async () => {
  if (userStore.isLoggedIn && !userStore.realName) {
    try {
      await userStore.getUserInfo()
    } catch (error) {
      console.error('获取用户信息失败:', error)
    }
  }
})
</script>

<style scoped>
.layout-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--gradient-bg);
}

.top-header {
  background: var(--bg-primary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  box-shadow: var(--shadow-sm);
  border-bottom: 1px solid var(--border-light);
  height: 60px;
}

.header-left {
  display: flex;
  align-items: center;
  flex: 1;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-primary);
  padding: 0 20px;
  margin-right: 20px;
  font-weight: 600;
}

.logo h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--primary-color);
}

.top-menu {
  border: none;
  flex: 1;
  background: transparent;
}

.top-menu :deep(.el-menu-item) {
  height: 60px;
  line-height: 60px;
  border-bottom: 2px solid transparent;
  color: var(--text-secondary);
  font-weight: 500;
  transition: var(--transition-fast);
}

.top-menu :deep(.el-menu-item:hover) {
  color: var(--primary-color);
  background: var(--bg-secondary);
}

.top-menu :deep(.el-menu-item.is-active) {
  color: var(--primary-color);
  border-bottom-color: var(--primary-color);
  background: var(--bg-secondary);
}

.top-menu :deep(.el-menu-item .el-icon) {
  color: inherit;
  margin-right: 5px;
}

.header-right {
  display: flex;
  align-items: center;
  margin-left: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 5px 10px;
  border-radius: var(--radius-md);
  transition: var(--transition-fast);
}

.user-info:hover {
  color: var(--primary-color);
  background: var(--bg-secondary);
}

.username {
  margin: 0 8px;
  color: inherit;
  font-weight: 500;
}

.main-content {
  background: var(--gradient-bg);
  padding: 0;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.breadcrumb-container {
  padding: 15px 20px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-light);
  box-shadow: var(--shadow-sm);
}

.breadcrumb-container :deep(.el-breadcrumb__item) {
  font-weight: 500;
}

.breadcrumb-container :deep(.el-breadcrumb__inner) {
  color: var(--text-secondary);
  transition: var(--transition-fast);
}

.breadcrumb-container :deep(.el-breadcrumb__inner:hover) {
  color: var(--primary-color);
}

.breadcrumb-container :deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: var(--text-primary);
  font-weight: 600;
}

.content-wrapper {
  padding: 20px;
  flex: 1;
}

/* 下拉菜单样式 */
.header-right :deep(.el-dropdown-menu) {
  background: var(--bg-primary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: 8px 0;
}

.header-right :deep(.el-dropdown-menu__item) {
  color: var(--text-primary);
  transition: var(--transition-fast);
  padding: 10px 20px;
}

.header-right :deep(.el-dropdown-menu__item:hover) {
  background: var(--bg-secondary);
  color: var(--primary-color);
}

.header-right :deep(.el-dropdown-menu__item.is-divided) {
  border-top: 1px solid var(--border-light);
  margin-top: 8px;
  padding-top: 12px;
}

.header-right :deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 8px;
  color: inherit;
}
</style>